package com.demo.webmagic.pageProcessor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.functors.ConstantFactory;

import com.demo.common.constant.ConstantDtoFlight;
import com.demo.common.model.Proxyip;
import com.demo.common.model._MappingKit;
import com.demo.webmagic.dto.DtoProxyIp;
import com.demo.webmagic.pipeline.PipelineProxyIp;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class PageProcessorProxy360 implements PageProcessor {

	private static final Log log = Log.getLog(PageProcessorProxy360.class);
	
	private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setCycleRetryTimes(3);
	
	@Override
	public Site getSite() {
		return site;
	}
	
	@Override
	public void process(Page page) {
		DtoProxyIp dtoProxyIp = new DtoProxyIp();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			List<Proxyip> proxyIpList = new ArrayList<Proxyip>();
			List<Selectable> ipList = page.getHtml().xpath("//div[@class='proxylistitem' AND @name='list_proxy_ip']/div/span/text()").nodes();
			for (int i = 0; i < ipList.size(); i++) {
				Proxyip proxyip = new Proxyip();
				String ip = ipList.get(i++).get().trim();
				String port = ipList.get(i++).get().trim();
				String type = ipList.get(i++).get().trim();
				String location = ipList.get(i++).get().trim();
				
				if(StrKit.notBlank(type) && "高匿".equals(type)) {
					proxyip.setIpStr(ip);
					proxyip.setPortStr(port);
					proxyip.getLong(location);
					proxyip.setValiDate(sdf.format(new Date()));
					proxyIpList.add(proxyip);
				}
				//System.out.println(ip+":"+port+"|"+type+"|"+location+"|"+valiDate);
				//System.out.println("============");
				i++;i++;i++;
			}
			
			dtoProxyIp.setCode(ConstantDtoFlight.code_success);
			dtoProxyIp.setProxyIpList(proxyIpList);
		} catch(Exception e){
			log.error(e.getMessage());
			dtoProxyIp.setCode(ConstantDtoFlight.code_fail);
		} finally {
			page.putField("result", dtoProxyIp);
		}
		/*DtoProxyIp dtoProxyIp = new DtoProxyIp();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			List<Proxyip> proxyIpList = new ArrayList<Proxyip>();
			String[] ips = page.getHtml().xpath("//body/text()").toString().split(" ");
			for (String ip : ips) {
				Proxyip proxyip = new Proxyip();
				String[] tmp = ip.split(":");
				if(tmp.length == 2) {
					proxyip.setIpStr(tmp[0]);
					proxyip.setPortStr(tmp[1]);
					
					proxyip.setValiDate(sdf.format(new Date()));
					proxyIpList.add(proxyip);
				}
			}
			dtoProxyIp.setCode(ConstantDtoFlight.code_success);
			dtoProxyIp.setProxyIpList(proxyIpList);
		} catch (Exception e) {
			log.error(e.getMessage());
			dtoProxyIp.setCode(ConstantDtoFlight.code_fno_invalid);
		} finally {
			page.putField("result", dtoProxyIp);
		}*/
	}
	
	public static void main(String[] args) {
		PropKit.use("a_little_config.txt");
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"));
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		_MappingKit.mapping(arp);
		
		dp.start();
		arp.start();
		
		String url = "http://www.proxy360.cn/default.aspx";
		Spider.create(new PageProcessorProxy360()).addUrl(url).addPipeline(new PipelineProxyIp()).start();
	}
}
